﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MINERP.DAL;
using MINERP.MODEL.FoundationModel;

namespace MINERP.BLL.InfoConfig
{
    public class TechCommomChargeItemBLL
    {
        /// <summary>
        /// 获取所有节点
        /// </summary>
        /// <returns></returns>
        public List<Tb_Tech_Info> GetAllTech()
        {
            List<Tb_Tech_Info> rtnList=new List<Tb_Tech_Info>();
            using (newSampleYPDBEntities db=new newSampleYPDBEntities())
            {
                List<tb_Tech> listTech = db.tb_Tech.ToList();
                foreach (tb_Tech list in listTech)
                {
                    rtnList.Add(new Tb_Tech_Info(){TechId = list.TechId,TechName = list.TechName});
                }
            }
            return rtnList;
        }
        /// <summary>
        /// 获取节点的收费项目
        /// </summary>
        /// <param name="intechId"></param>
        /// <returns></returns>
        public List<TechCommonChargeItem_Info> GetTechComm(int intechId)
        {
            List<TechCommonChargeItem_Info> rtnList=new List<TechCommonChargeItem_Info>();
             using (newSampleYPDBEntities db=new newSampleYPDBEntities())
             {
                 List<CommonChargeItem> listComm = db.CommonChargeItem.ToList();
                 tb_Tech tech = db.tb_Tech.FirstOrDefault(p => p.TechId == intechId);
                 tech.CommonChargeItem.Load();
                 List<CommonChargeItem> techItems = tech.CommonChargeItem.ToList();
                 foreach (CommonChargeItem item in listComm)
                 {
                     TechCommonChargeItem_Info info=new TechCommonChargeItem_Info();
                     info.CommonChargeItemId = item.CommonChargeItemId;
                     info.ItemName = item.ItemName;
                    
                     CommonChargeItem comm =
                         techItems.FirstOrDefault(p => p.CommonChargeItemId == item.CommonChargeItemId);
                     if(comm!=null)
                     {
                         info.TechId = tech.TechId;
                         info.isTechCommom = true;
                         info.isTechCommom2 = true;
                     }
                     rtnList.Add(info);
                 }
                 return rtnList;
             }
        }

        public int UpdataTechCommonChargeIte(int inTechid,List<TechCommonChargeItem_Info> inList)
        {
            using(newSampleYPDBEntities db=new newSampleYPDBEntities())
            {
                foreach (TechCommonChargeItem_Info list in inList)
                {
                    if(list.isTechCommom&&list.isTechCommom2)
                    {
                        //已有的不更新
                    }else if(list.isTechCommom&&!list.isTechCommom2)
                    {
                        //新添加
                        tb_Tech tech = db.tb_Tech.FirstOrDefault(p => p.TechId == inTechid);
                        CommonChargeItem cci = db.CommonChargeItem.FirstOrDefault(p => p.CommonChargeItemId==list.CommonChargeItemId);
                        tech.CommonChargeItem.Load();
                        tech.CommonChargeItem.Add(cci);


                    } else if(!list.isTechCommom&&list.isTechCommom2)
                    {
                        //删除的
                        tb_Tech tech = db.tb_Tech.FirstOrDefault(p => p.TechId == inTechid);
                        CommonChargeItem cci = db.CommonChargeItem.FirstOrDefault(p => p.CommonChargeItemId == list.CommonChargeItemId);
                        tech.CommonChargeItem.Load();
                        tech.CommonChargeItem.Remove(cci);
                    }
                    else
                    {
                        //未有 不更新
                    }
                    db.SaveChanges();
                }
            }
            return 0;
        }

    }
    public class TechCommonChargeItem_Info
    {
        public int TechId { get; set; }
        public int CommonChargeItemId { get; set; }
        public string ItemName { get; set; }
        public bool isTechCommom { get; set; }
        public bool isTechCommom2 { get; set; }//更新时对比用
    }
}
